const pgp = require('pg-promise')();

const Mqtt = require('mqtt')


module.exports = app => {
  // pg-promise
  app.db = pgp(app.config.database.pg);

  app.config.coreMiddleware.push('validate');
//  console.log( app.config.coreMiddleware );
  console.log( app.config.mqtt );

  var host = app.config.mqtt.host
  var port = app.config.mqtt.port
  app.beforeStart( async() =>{
       app.monitorChannel = "monitor"

       console.log( "-----------" );
       app.deviceChannel = "device"

       app.webClientChannel = "webclient"

       app.mqtt = await ( new Promise( function( resolve, reject ) {
         var mqtt = Mqtt.connect( "mqtt://" + host )
         mqtt.on( "connect" , (ack) => {
           console.log( "##### connect to ", host ) ;
           resolve( mqtt )
         });
       }))
  })

}

